package com.rongshu.view.userInfo.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Common;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.userInfo.adapter.CardBagAdapter;
import com.rongshu.view.userInfo.entity.CardBagInfo;
import com.rongshu.view.userInfo.iview.CardBagView;
import com.rongshu.view.userInfo.presenter.CardBagPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class CardBagActivity extends BaseActivity<CardBagPresenter> implements CardBagView {
    @BindView(R.id.txt_top_title)
    TextView txt_top_title;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.layout_empty)
    RelativeLayout layout_empty;

    //manager
    private LinearLayoutManager manager = null;
    //adapter
    private CardBagAdapter adapter = null;
    //map
    private Map<String, Object> map = null;

    @Override
    protected int initLayout() {
        return R.layout.activity_card_bag_view;
    }

    @Override
    protected void initView() {
        //set presenter
        setPresenter(new CardBagPresenter(this), this);
        //top
        txt_top_title.setText(getString(R.string.my_card_bag));
        //manager
        manager = new LinearLayoutManager(this);
        //adapter
        adapter = new CardBagAdapter(this);
        //recyclerview
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
        refreshLayout.setHeaderHeight(0);
        refreshLayout.setFooterHeight(0);
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setEnableHeaderTranslationContent(false);
        refreshLayout.setEnableAutoLoadmore(false);
        refreshLayout.setEnableFooterTranslationContent(false);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void updateViews(boolean isRefresh) {
        getMyProps();
    }

    /**
     * 获取我的卡包
     */
    private void getMyProps() {
        int user_id = UserInfoUtils.getUserId();
        map = new HashMap<>();
        map.put("user_id", user_id);
        presenter.getMyProps(user_id, Common.encryptionSign(map));
        map.clear();
    }

    @OnClick({R.id.layout_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_back:
                finish();
                break;
        }
    }

    /**
     * 获取我的卡包
     *
     * @param cardBagInfo
     */
    @Override
    public void onMyProps(CardBagInfo cardBagInfo) {
        if (cardBagInfo.getErrCode() == 0) {
            List<CardBagInfo.ResponseBean> responseBeanList = cardBagInfo.getResponse();
            if (responseBeanList != null && !responseBeanList.isEmpty()) {
                refreshLayout.finishRefresh();
                layout_empty.setVisibility(View.GONE);
                refreshLayout.setVisibility(View.VISIBLE);
                if (adapter != null) {
                    adapter.setCardBagInfos(responseBeanList);
                    adapter.notifyDataSetChanged();
                }
            } else {
                refreshLayout.finishRefresh();
                layout_empty.setVisibility(View.VISIBLE);
                refreshLayout.setVisibility(View.GONE);
            }
        } else {
            toast(cardBagInfo.getErrMsg());
        }
    }
}
